package com.app.newiptv.promote;

import com.app.newiptv.activity.Contast;
import com.app.newiptv.bean.RequestDto;
import com.app.newiptv.callback.IRequestLisenter;
import com.app.newiptv.utils.APPLog;
import com.app.newiptv.utils.OkGoUtil;

import org.json.JSONException;
import org.json.JSONObject;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * Created by wendal on 2017/10/22.
 */

public class AuthRunnable  {
    private AuthCallBack authCallBack;
    public void setCallBack(AuthCallBack callBack){
        this.authCallBack = callBack;
    }
    public void loadData(String url,String username,String password) {

        OkGoUtil<String> okGoUtil = new OkGoUtil<String>(String.class, OkGoUtil.TYPE_STRING, new IRequestLisenter() {
            @Override
            public void onSuccessData(RequestDto dto) {
                //AuthBean  authBean = (AuthBean) dto.getData();\
                String result = dto.getResult();
                APPLog.printInfo("onSuccessData==>:" + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String code = jsonObject.optString("code");
                    APPLog.printInfo("CODE==>:"+ code);
                    if(code.equals("200")){
                        String token = jsonObject.optString("token");
                        Contast.USE_TOKEN = token;
                        if(authCallBack!=null){
                            authCallBack.completeUpdateInfo();
                        }
                    }
                } catch (JSONException e) {

                }
            }

            @Override
            public void onErrer(int code, String errMsg) {
                APPLog.printInfo("onErrer===");
                authCallBack.error();
            }
        });
        //okGoUtil.requestGet(url);
        APPLog.printInfo("name:" +username);
        okGoUtil.params("username",username);
        okGoUtil.params("password",password);
        okGoUtil.params("mac",getLocalEthernetMacAddress());
        okGoUtil.requestPost(url);
        APPLog.printInfo("mac==>:"+ getLocalEthernetMacAddress());
    }
    public static String getLocalEthernetMacAddress() {
        String mac=null;
        try {
            Enumeration localEnumeration= NetworkInterface.getNetworkInterfaces();
            while (localEnumeration.hasMoreElements()) {
                NetworkInterface localNetworkInterface=(NetworkInterface) localEnumeration.nextElement();
                String interfaceName=localNetworkInterface.getDisplayName();
                if (interfaceName==null) {
                    continue;
                }
                if (interfaceName.equals("eth0")) {
                    mac=convertToMac(localNetworkInterface.getHardwareAddress());
                    if (mac!=null&&mac.startsWith("0:")) {
                        mac="0"+mac;
                    }
                    break;
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return mac;
    }

    private static String convertToMac(byte[] mac) {
        StringBuilder sb=new StringBuilder();
        for (int i=0; i<mac.length; i++) {
            byte b=mac[i];
            int value=0;
            if (b>=0&&b<=16) {
                value=b;
                sb.append("0"+Integer.toHexString(value));
            } else if (b>16) {
                value=b;
                sb.append(Integer.toHexString(value));
            } else {
                value=256+b;
                sb.append(Integer.toHexString(value));
            }
            if (i!=mac.length-1) {
                sb.append(":");
            }
        }
        return sb.toString();
    }

}
